package com.bjsxt.controller;

import com.bjsxt.disruptor.OrderEvent;
import com.bjsxt.disruptor.OrderEventHandler;
import com.bjsxt.domain.DepthItemVo;
import com.bjsxt.feign.OrderBooksFeignClient;
import com.bjsxt.model.MergeOrder;
import com.bjsxt.model.Order;
import com.lmax.disruptor.EventHandler;
import net.sf.jsqlparser.statement.merge.Merge;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import sun.reflect.generics.tree.Tree;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;


@RestController
public class MatchController implements OrderBooksFeignClient {

    @Autowired
    private EventHandler<OrderEvent>[] eventHandlers;

    @GetMapping("/match/order")
    public TreeMap<BigDecimal, MergeOrder> getTradeData(@RequestParam(required = true) String symbol, Integer orderDirection){
        for(EventHandler<OrderEvent> eventEventHandler :eventHandlers){
            OrderEventHandler orderEventHandler = (OrderEventHandler) eventEventHandler;
            if (orderEventHandler.getSymbol().equals(symbol)){
                return orderEventHandler.getOrderBooks().getCurrentLimitPrices(com.bjsxt.enums.OrderDirection.getOrderDirection(orderDirection));
            }

        }
        return null;
    }

    @Override
    public Map<String, List<DepthItemVo>> querySymbolDepth(String symbol) {

        for(EventHandler<OrderEvent> eventEventHandler :eventHandlers){
            OrderEventHandler orderEventHandler = (OrderEventHandler) eventEventHandler;
            if(orderEventHandler.getSymbol().equals(symbol)){
                HashMap<String, List<DepthItemVo>> deptmap = new HashMap<>();
                deptmap.put("asks",orderEventHandler.getOrderBooks().getSellTradePlate().getItems());
                deptmap.put("bids",orderEventHandler.getOrderBooks().getBuyTradePlate().getItems());
                return deptmap;
            }
        }
        return null;
    }
}
